package com.company.myapplication.utils;

import android.text.TextUtils;

import com.company.myapplication.R;
import com.company.myapplication.app.MyApp;

import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 字符串处理工具类
 */
public class StringUtils {
    public static boolean isListValid(List<?> t) {
        return t != null && t.size() > 0;
    }

    public static String getPermissionName(String per) {
        switch (per) {
            case "android.permission.READ_PHONE_STATE":
                return MyApp.getInstanceApp().getString(R.string.phone_state);
        }
        return "";
    }

    /**
     * 数字和英文字母的匹配判断
     *
     * @param str 输入串
     */
    public static boolean strMatchEN(CharSequence str) {
        return Pattern.matches("^[A-Za-z0-9]+$", str);
    }

    /**
     * 数字的匹配判断
     *
     * @param str 输入串
     */
    public static boolean strMatchNum(String str) {
        return Pattern.matches("^-?\\d+(\\.\\d+)?$", str);
    }

    public static String formatFloat(float num) {
        DecimalFormat df = new DecimalFormat("#.0");
        return df.format(num);
    }

    public static String formatFloatValue(float num) {
        DecimalFormat df = new DecimalFormat("0.0");
        return df.format(num);
    }

    public static boolean isequals(String A, String B) {
        if (A == null && B != null) {
            return false;
        }
        if (A != null && B == null) {
            return false;
        }

        if (A == null && B == null) {
            return true;
        } else {
            return TextUtils.equals(A, B);
        }

    }

    public static int getParseInt(String valule, int defaultValue) {
        try {
            int i = Integer.parseInt(valule);
            return i;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }


        return defaultValue;
    }


}
